#!/bin/bash

git pull

# 检查是否有冲突
if git status --porcelain | grep -q "^U"; then
    echo "有冲突，请手动解决冲突"
    exit 1
fi

git merge develop -m "合并 develop 分支"

if [ $? -ne 0 ]; then
  echo "有冲突请手动合并"
  # 取消合并
  git merge --abort
  exit
fi


# 先执行更新操作，避免有插件更新
pnpm i

#nuxt clean
pnpm build

# 判断build命令是否成功
if [ $? -eq 0 ]; then
  echo "Build成功！"
else
  echo "Build失败！"
  exit
fi
# 判断dist文件夹是否存在
if [ ! -d "./dist" ]; then
  echo "dist文件夹不存在"
  exit
fi

# 开始压缩dist文件夹成一个zip包
echo "正在压缩dist文件夹......"

zip -r dist.zip ./dist

# 等待压缩完成
while [ ! -f "./dist.zip" ]
do
  sleep 1
done

echo "dist文件夹已成功压缩为dist.zip！"

if [ -z "${localBranchName+x}" ]; then
  localBranchName=""
else
  localBranchName="$localBranchName"
fi

# 执行git相关命令
git add .
git commit -m "$localBranchName 部署commit"
git push


# 是否切换回原来分支
echo "部署已完成准备切换回develop分支,窗口五秒后关闭"
sleep 2s

clear
# 展示最新记录
git log -1
git checkout develop
sleep 5s
exit
